package com.example.sunlight.interneteverything;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;

import com.example.sunlight.interneteverything.View.ViewLeaf;

import java.util.Random;

import data.Datas;

/**
 * Created by SunLight on 2017/8/16.
 */

public class NewAgriculture extends Activity {
    ViewLeaf viewLeaf1, viewLeaf3, viewLeaf2, viewLeaf;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0x123:
                    viewLeaf1.setNumber(Datas.Hum);
                    viewLeaf3.setNumber(Datas.Tem);

                    break;
                case 0x456:
                    if (state == false) {
                        viewLeaf2.setNumber("开启");
                        state = true;
                    } else {
                        viewLeaf2.setNumber("关闭");
                        state = false;
                    }
                    break;
                case 0x789:
                    if (max == 100) {
                        max = 0;
                        viewLeaf.setNumber(max + "%");
                    } else {
                        max++;
                        viewLeaf.setNumber(max + "%");
                    }
                    break;
            }
        }
    };
    boolean state = false;
    int max = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newagriculture);
        FindView();
        textUpdate = new TextUpdate();
        textUpdate.start();
        textChang = new TextChang();
        textChang.start();
        gvChang = new GvChang();
        gvChang.start();
    }

    TextUpdate textUpdate;
    TextChang textChang;
    GvChang gvChang;

    public void FindView() {
        viewLeaf1 = (ViewLeaf) findViewById(R.id.viewLeaf1);
        viewLeaf3 = (ViewLeaf) findViewById(R.id.viewLeaf3);
        viewLeaf2 = (ViewLeaf) findViewById(R.id.viewLeaf2);
        viewLeaf = (ViewLeaf) findViewById(R.id.viewLeaf);

    }

    class GvChang extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(1000);
                    handler.sendEmptyMessage(0x789);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    class TextChang extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(5000);
                    handler.sendEmptyMessage(0x456);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    class TextUpdate extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(45);
                    handler.sendEmptyMessage(0x123);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
